package com.classbook.violet.baigiang6.subject;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.xml.parsers.SAXParserFactory;
 

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
 

import android.util.Log;
 
public class SubjectParser {
	
	private XMLReader xmlReader;
	private List<SubjectNode> listStudy;
	public Boolean isParser = false;
	public String error;
	HashMap<String, String> sub_map = new HashMap<String, String>();
	private int lop = 7;
	
	public SubjectParser (InputStream is){
		sub_map.put("Đại số "+lop, "THCS/Dai so/Dai so "+lop+"/");
		sub_map.put("Hình học "+lop, "THCS/Hinh hoc/Hinh hoc "+lop+"/");
		sub_map.put("Ngữ văn "+lop, "THCS/Ngu Van/Ngu Van "+lop+"/");
		sub_map.put("Tiếng Anh "+lop, "THCS/Tieng Anh/Tieng Anh "+lop+"/");
		sub_map.put("Vật lí "+lop, "THCS/Vat li/Vat li "+lop+"/");
		sub_map.put("Sinh học "+lop, "THCS/Sinh hoc/Sinh hoc "+lop+"/");
		sub_map.put("Lịch sử "+lop, "THCS/Lich su/Lich su "+lop+"/");
		sub_map.put("Địa lí "+lop, "THCS/Dia li/Dia li "+lop+"/");
		
		listStudy = new ArrayList<SubjectNode>();
		
		 try {
	            // create a XMLReader from SAXParser
	            xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
	            // create a StudyHandler too
	            SubjectHandler subjectHandler = new SubjectHandler();
	            // apply handler to the XMLReader
	            xmlReader.setContentHandler(subjectHandler);
	            // the process starts
	            xmlReader.parse(new InputSource(is));
	            
	            listStudy = subjectHandler.getListStudy();
	            
	            //Log.i("STUDY SIZE", "" + listStudy.size());
	            isParser = true;
	 
	        } catch(Exception ex) {
	        	isParser = false;
	        	error = ex.toString();
	            Log.d("XML", "SubjectParser parse() failed: " + ex.toString());
	        }
	}
	
	public  List<SubjectNode> parseStudy (){
		return listStudy;
	}
	
	public boolean IsParserSuccess(){
		return isParser;
	}
 
    public List<LessonNode> parseLesson(String subject) {
        List<LessonNode> lessonList = new ArrayList<LessonNode>();
        SubjectNode mySubject = getStudyNode(subject);
        if (mySubject != null){
        for(int i = 0; i < mySubject.unitList.size(); i++){
        	//Log.i("Lession", myStudy.lessionList.get(i).label);
        	for (int j = 0; j < mySubject.unitList.get(i).lessonList.size(); j++){
        		//Log.i("Subject", myStudy.lessionList.get(i).listSub.get(j).label);
        		LessonNode temp_node = mySubject.unitList.get(i).lessonList.get(j);
        		temp_node.file_link = sub_map.get(subject);
        		lessonList.add(temp_node);
        	}
        }
        }
        Log.i("SUBJECT SIZE", "" + lessonList.size());
        return lessonList;
    }
    
    public  List<UnitNode> parseUnit(String study) {
        List<UnitNode> unitList = new ArrayList<UnitNode>();
        SubjectNode myStudy = getStudyNode(study);
        for(int i = 0; i < myStudy.unitList.size(); i++){
        	unitList.add(myStudy.unitList.get(i));
        }
        //Log.i("LESSION SIZE", "" + lessionList.size());
        return unitList;
    }
    
    private SubjectNode getStudyNode (String label){
    	SubjectNode myStudy = null;
    	for (int i = 0; i < listStudy.size(); i++){
    		if (listStudy.get(i).label.contains(label)){
    			myStudy = listStudy.get(i);
    		}
    	}
    	return myStudy;
    }
}
